<?php
/**
 * <pre>
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT      : rubik-base
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 *
 * </pre>
 * 
 * @version 		1.0
 * @author			Dang Nhon Hoa <hoadn@rubik.com.vn>
 * @access			public
 * @package			rubik.base.helper
 *
 * Created on	: May 25, 2008
 */
RCoreImporter::cls("helper.RubikHelper" );

class RHtmlHelper {
	
	/**
	 * Create the HTML check box list
	 * 
	 * @param string $name name of checkbox
	 * @param array $arrayValue array content the value and View Name. it must be in define: ($value=>$viewName)
	 * @param $arraySelected array The selected value
	 * @param $extAttributes
	 * @param $htmlIdPrefix
	 * @param $wrapper string wrapper is the name tag of wrapper maye: 'div', 'span'
	 * @param $wraperAttributes the attribute for the wrapper
	 * @return string HTML
	 */
	static function createCheckBoxListHtml($name, $arrayValue, $arraySelected = null, 
									$extAttributes = "", $htmlIdPrefix = "", $wrapper=null, $wraperAttributes = null) {
		if (strpos($name, '[]') === false) {
			$name .= '[]';
		}
		
		if (!$arraySelected) {
			$arraySelected = array();
		}
		
		foreach ( $arrayValue as $value => $viewName ) {
			$selected = false;
			foreach ($arraySelected as $selVal) {
				if (strval($selVal) == strval($value)) {
					$selected = true;
					break;
				}
			}
			$id = $htmlIdPrefix.$name.$value;
			$id = str_replace('[]', '_', $id);
			
			$checked = '';
			if ($selected) {
				$checked = " checked=\"checked\"";
			}
			
			$itemHtml = "\t<input type=\"checkbox\" {$checked} value=\"$value\" id=\"{$id}\" name=\"{$name}\" /><label for=\"{$id}\">$viewName</label>\n";
			if ($wrapper) {
				$itemHtml = "<{$wrapper} {$wraperAttributes}>{$itemHtml}</{$wrapper}>";
			}
			
			$result .= $itemHtml;
		}
		return $result;
	}
	/**
	 * Create the HTML select box (combobox)
	 *
	 * @param string $name name of combobox
	 * @param array $arrayValue array content the value and View Name. it must be in define: ($value=>$viewName)
	 * @param string $selectedValue the selected value
	 * @param string $extAttributes more attributes string for this component
	 * @param string $htmlId the id of this HTML. by default it will be equal to the $name
	 * @param string $cssCls the CSS class of this control
	 * @param string $style the style string if this control
	 * @param boolean $isMultipleSelect
	 * 
	 * @return string The return HTML
	 */
	static function createSelectHtml($name, $arrayValue, $arrayOrObject = "", $extAttributes = "", 
							$htmlId = "", $cssCls = "", $style = "", $isMultipleSelect = false, $shouldGetValueFromName = true) {
		if (empty ( $htmlId )) {
			$htmlId = $name;
		}
		if ($shouldGetValueFromName) {
			$selectedValue = RubikHelper::getValueFrom ( $arrayOrObject, $name );
		}
		else {
			$selectedValue = $arrayOrObject;
		}

		if (! empty ( $cssCls )) {
			$extAttributes .= "class=\"$cssCls\" ";
		}
		if (! empty ( $style )) {
			$extAttributes .= "style=\"$style\" ";
		}
		if ($isMultipleSelect) {
			$extAttributes .= ' multiple="multiple"';
			$name .= '[]';
		}
		
		if (!$arrayValue) {
			$arrayValue = array();
		}
		
		$result = "<select name=\"$name\" id=\"$htmlId\" $extAttributes >\n";
		if (is_array($selectedValue)) {
			foreach ( $arrayValue as $value => $viewName ) {
				$selected = false;
				foreach ($selectedValue as $selVal) {
					if (strval($selVal) == strval($value)) {
						$selected = true;
						break;
					}
				}
				if ($selected) {
					$result .= "\t<option value=\"$value\" class=\"selected\" selected=\"true\">$viewName</option>\n";
				}
				else {
					$result .= "\t<option value=\"$value\">$viewName</option>\n";
				}
			}
		}
		else {
			if (count($arrayValue)) {
				foreach ( $arrayValue as $value => $viewName ) {
					if (strval($value) === strval($selectedValue)) {
						$result .= "\t<option value=\"$value\" class=\"selected\" selected=\"true\">$viewName</option>\n";
					} else {
						$result .= "\t<option value=\"$value\">$viewName</option>\n";
					}
				}
			}
		}
		$result .= "</select>";
		return $result;
	}
	
	/**
	 * Print the recordset into table
	 * Normaly this should be use for debuging database
	 *
	 * @param recordset $arrayRecord
	 * @return string html table of input array
	 */
	static function printDBTable($arrayRecord) {
		if (! is_array ( $arrayRecord )) {
			return;
		}
		
		$first = true;
		
		$result = '<table width="100%">';
		foreach ( $arrayRecord as $rec ) {
			if ($first) {
				$result .= "<th>";
				foreach ( $rec as $k => $v ) {
					$result .= "<td>$k</td>";
				}
				$result .= "</th>";
				$first = false;
			}
			$result .= "<tr>";
			foreach ( $rec as $k => $v ) {
				$result .= "<td>$v</td>";
			}
			$result .= "</tr>";
		}
		$result .= '</table>';
		return $result;
	}
	
	/**
	 * Create the Hidden input HTML
	 *
	 * @param string $inputName
	 * @param string/array $valueOrArrayContain string -> the value of hidden
	 * 											array/object -> array that contain the value. normaly it is: $_GET or $_POST or $_REQUEST
	 * @param string $htmlId
	 * @return string
	 */
	static function createHiddenHtml($inputName, $valueOrArrayContain, $htmlId = null) {
		$valueOrArrayContain = RubikHelper::getValueFrom ( $valueOrArrayContain, $inputName );
		if ($htmlId === null) {
			$htmlId = $inputName;
		}
		return "<input type=\"hidden\" name=\"$inputName\" id=\"$htmlId\" value=\"$valueOrArrayContain\" />";
	}
	
	/**
	 * Create the Checkbox input HTML
	 *
	 * @param string $inputName
	 * @param string/array $valueOrArrayContain string -> the value of hidden
	 * 											array/object -> array that contain the value. normaly it is: $_GET or $_POST or $_REQUEST
	 * @param string $htmlId
	 * @return string
	 */
	static function createCheckBoxWithLabel($inputName,$labelName, $valueOrArrayContain, $htmlId = null, $extendAttribute = "") {
		$valueOrArrayContain = RubikHelper::getValueFrom ( $valueOrArrayContain, $inputName );
		if ($htmlId === null) {
			$htmlId = $inputName;
		}
		if (intval ( $valueOrArrayContain ) !== 0) {
			$check = 'checked="checked"';
		}
		return "<input type=\"checkbox\" name=\"$inputName\" id=\"$htmlId\" $check $extendAttribute value=\"1\" /><label for=\"$htmlId\">$labelName</label>";
	}
	
	/**
	 * Create the Checkbox input HTML
	 *
	 * @param string $inputName
	 * @param string/array $valueOrArrayContain string -> the value of hidden
	 * 											array/object -> array that contain the value. normaly it is: $_GET or $_POST or $_REQUEST
	 * @param string $htmlId
	 * @return string
	 */
	static function createCheckBox($inputName, $valueOrArrayContain, $htmlId = null, $extendAttribute = "", $value="1") {
		$valueOrArrayContain = RubikHelper::getValueFrom ( $valueOrArrayContain, $inputName );
		if ($htmlId === null) {
			$htmlId = $inputName;
		}
		if (intval ( $valueOrArrayContain ) !== 0) {
			$check = 'checked="checked"';
		}
		return "<input type=\"checkbox\" name=\"$inputName\" id=\"$htmlId\" $check $extendAttribute value=\"{$value}\" />";
	}
	
	/**
	 * Create the Checkbox input HTML
	 *
	 * @param string $inputName
	 * @param string/array $valueOrArrayContain string -> the value of hidden
	 * 											array/object -> array that contain the value. normaly it is: $_GET or $_POST or $_REQUEST
	 * @param string $htmlId
	 * @return string
	 */
	static function createValueCheckBox($inputName, $valueOrArrayContain, $shouldCheck, $htmlId = null, $extendAttribute = "") {
		$valueOrArrayContain = RubikHelper::getValueFrom ( $valueOrArrayContain, $inputName );
		if ($htmlId === null) {
			$htmlId = $inputName;
		}
		if ($shouldCheck) {
			$check = 'checked="checked"';
		}
		return "<input type=\"checkbox\" name=\"$inputName\" id=\"$htmlId\" $check $extendAttribute value=\"{$valueOrArrayContain}\" />";
	}
	
	/**
	 * Create the Checkbox input HTML
	 *
	 * @param string $inputName
	 * @param string/array $valueOrArrayContain string -> the value of hidden
	 * 											array/object -> array that contain the value. normaly it is: $_GET or $_POST or $_REQUEST
	 * @param string $htmlId
	 * @return string
	 */
	static function createYesNoRadio($inputName, $valueOrArrayContain, $defaultvalue = 0, $htmlId = null , $cssCls = '') {
		//		$valueOrArrayContain = RubikHelper::getValueFrom($arrayOrObject, $inputName);
		if ($htmlId === null) {
			$htmlId = $inputName;
		}
		
		$result = "";
		if ($defaultvalue !== 0) {
			$result .= "<input type=\"radio\" {$cssCls} name=\"$inputName\" id=\"{$htmlId}_yes\" checked=\"checked\" value=\"1\" />Yes ";
			$result .= "<input type=\"radio\" {$cssCls} name=\"$inputName\" id=\"{$htmlId}_no\" value=\"0\" />No ";
		} else {
			$result .= "<input type=\"radio\" {$cssCls} name=\"$inputName\" id=\"{$htmlId}_yes\" value=\"1\" />Yes ";
			$result .= "<input type=\"radio\" {$cssCls} name=\"$inputName\" id=\"{$htmlId}_no\" checked=\"checked\" value=\"0\" />No ";
		}
		return $result;
	}
	
	/**
	 * Create the HTML select box (combobox)
	 *
	 * @param string $name name of combobox
	 * @param array $arrayValue array content the value and View Name. it must be in define: ($value=>$viewName)
	 * @param string $selectedValue the selected value
	 * @param string $extAttributes more attributes string for this component
	 * @param string $htmlId the id of this HTML. by default it will be equal to the $name
	 * @param string $cssCls the CSS class of this control
	 * @param string $style the style string if this control
	 * 
	 * @return string The return HTML
	 */
	static function createListRadioButtonHtml($name, $arrayValue, $arrayOrObject = "", $extAttributes = "", $separator = "", $htmlId = "", $cssCls = "", $style = "") {
		if (empty ( $htmlId )) {
			$htmlId = $name;
		}
		$selectedValue = RubikHelper::getValueFrom ( $arrayOrObject, $name );
		if (! empty ( $cssCls )) {
			$extAttributes .= "class=\"$cssCls\" ";
		}
		if (! empty ( $style )) {
			$extAttributes .= "style=\"$style\" ";
		}
		
		$result = "<div id=\"$htmlId\" $extAttributes >\n";
		$i = 0;
		foreach ( $arrayValue as $value => $viewName ) {
			$i ++;
			if ($value == $selectedValue) {
				$result .= "\t<input type=\"radio\" value=\"$value\" id=\"{$name}{$i}\" name=\"$name\" checked=\"checked\" /> <label for=\"{$name}{$i}\">$viewName</label>$separator\n";
			} else {
				$result .= "\t<input type=\"radio\" value=\"$value\"  id=\"{$name}{$i}\" name=\"$name\" /> <label for=\"{$name}{$i}\">$viewName</label> $separator\n";
			}
		}
		$result .= "</div>";
		return $result;
	}

	/**
	 * Create the HTML select box (combobox)
	 *
	 * @param string $name name of combobox
	 * @param array $arrayValue array content the value and View Name. it must be in define: ($value=>$viewName)
	 * @param string $arrayOrObject the selected value
	 * @param string $extAttributes more attributes string for this component
	 * @param string $htmlId the id of this HTML. by default it will be equal to the $name
	 * 
	 * @return string The return HTML
	 */
	static function createRadioButtonHtml($name, $value, $arrayOrObject = "", $extAttributes = "", $htmlId = "") {
		if (empty ( $htmlId )) {
			$htmlId = $name;
		}
		$selectedValue = RubikHelper::getValueFrom ( $arrayOrObject, $name );
		
		if ($selectedValue == $value) {
			$checked = 'checked="checked"';
		}
		return "\t<input type=\"radio\" value=\"$value\" id=\"{$htmlId}\" name=\"$name\" {$extAttributes} {$checked} />\n";
	}

	static function addLangForLink($link) {
		$lang = SystemSite::getLanguage ();
		$config = SystemSite::getSiteConfig ();
		if ($lang != $config->language && in_array ( $lang, $config->language_array )) {
			if (strpos ( $link, '?lang=' ) === false) {
				if (strpos ( $link, '&lang=' ) === false) {
					$link .= "?lang=" . $lang;
				}
			}
			else if (strpos ( $link, '&lang=' ) === false) {
				$link .= "&lang=" . $lang;
			}
		}
		return $link;
	}
	
	/**
	 * Create flash object on html page
	 *
	 * @param str $swfUrl: Link to flash file
	 * @param int $width
	 * @param int $height
	 * @param aray: $attribute, if you want to add flashvar, name of flash object...
	 * @param str $objectMode: use 'html' to use only html code, and if you have AC_RunActiveContent.js use 'js' Recommend use 'js' type for validator XHTML and more feature for Flash Object
	 * @return html object
	 */
	static function createFlashObject($swfUrl, $width = null, $height = null, $objectMode = 'html', $attribute = array()) {
		$nameElement = 'flashObject';
		
		$flashVar = '';
		if($attribute['name']) {
			$nameElement = $attribute['name'];
			unset($attribute['name']);
		}
		if($attribute['id']) {
			$id = $attribute['id'];
			unset($attribute['id']);
		}
		
		if($objectMode == 'html') {
			if ($width) {
				$width = 'width="'.$width.'"';
			}
			if ($height) {
				$height = 'height="'.$height.'"';
			}
			
			foreach ($attribute as $key=>$val) {
				$objParmas .= ' <param name="'.$key.'" value="'.$val.'" />';
				$embParmas .= $key.'="'.$val."\" \n";
			}
			
			$return = '
			<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" '.$width.' '.$height.' id="'.$nameElement.'" align="middle">
				<param name="movie" value="'.$swfUrl.'" />'.
				$objParmas.
				'<embed src="'.$swfUrl.'" '.
					$width.' '.
					$height.
					' name="'.$nameElement.'" '.
					$embParmas.
					' id="'.$nameElement.'_emb" align="middle" 
					type="application/x-shockwave-flash" 
					pluginspage="http://www.macromedia.com/go/getflashplayer" 
				/>
			</object>';
		} else if ($objectMode == 'js') {
			if ($width) {
				$width = "'width', '{$width}',";
			}
			if ($height) {
				$height = "'height', '{$height}',";
			}
			$swfUrlJS = substr($swfUrl, 0, strlen($swfUrl) - 4);
			
			foreach ($attribute as $key=>$val) {
				$embParmas .= "'$key', '$val', \n";
			}
			// Please import AC_RunActiveContent.js first
			// Use: SysResponse::addJavascriptFile(SYS_ROOT_URL.'libraries/js/AC_RunActiveContent.js'); to import js file
			SysResponse::addJavascriptFile(SYS_ROOT_URL.'libraries/js/AC_RunActiveContent.js'); 
			$return = "<script type=\"text/javascript\">
							if (AC_FL_RunContent == 0) {
								alert(\"This page requires AC_RunActiveContent.js.\");
							} else {
								AC_FL_RunContent(
									'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0',
									{$width}
									{$height}
									{$embParmas}
									'src', '{$swfUrlJS}',
									'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
									'id', '{$nameElement}_player',
									'name', '{$nameElement}',
									'menu', 'true',
									'movie', '{$swfUrlJS}',
									'salign', ''
									); //end AC code
							}
						</script>";
		}
		
		return $return;
	}	

	/**
	 * Get the html standard video player
	 *
	 * @param string $flvFile
	 * @param int $width
	 * @param int $height
	 * @param string $objectMode
	 * @param attribute $attribute
	 */
	static function createFlashVideoPlayer($flvFile, $width = null, $height = null, $objectMode = 'html', $attribute = array()) {
		$swfFile = 'player.swf';
		$flashVar = "file={$flvFile}";
		$swfFile	= SYS_ROOT_URL."libraries/player/video/{$swfFile}";
		$attribute['FlashVars'] .= $flashVar;
		
		return RHtmlHelper::createFlashObject($swfFile, $width, $height, $objectMode, $attribute);
	}
	
	static function createCreditExpiredCtrl($arrOrObject, $monthName='expDateMonth', $yearName='expDateYear') {
		for ($i=1; $i <= 12; $i++) {
			$arrMonth[$i] = $i;
		}
		$monthHtml = self::createSelectHtml($monthName, $arrMonth, $arrOrObject);

		$year = date('Y') - 1;
		for ($i=0; $i < 12; $i++) {
			$arrYear[$year + $i] = $year + $i;
		}
		$yearHtml = self::createSelectHtml($monthName, $arrYear, $arrOrObject);
		
		return "{$monthHtml} {$yearHtml}";
	} 
}
?>
